#!/bin/sh /etc/rc.common
# Copyright (C) 2021 ImmortalWrt

. /lib/functions.sh
. /lib/functions/procd.sh

USE_PROCD=1

START=90
STOP=10

init_conf() {
	config_load "gecoosac"
	config_get "program_path" "config" "program_path" "/usr/bin/gecoosac"
	config_get "db_dir" "config" "db_dir" "/etc/gecoosac/"
	config_get "upload_dir" "config" "upload_dir" "/etc/gecoosac/upload/"
	config_get "enabled" "config" "enabled" "0"
	config_get "port" "config" "port" "60650"
	config_get "log" "config" "log" "0"
}

start_service() {
	init_conf
	[ "${enabled}" == "1" ] || return 0
	procd_open_instance gecoosac

	procd_set_param command $program_path
	procd_append_param command -f "${upload_dir}"
	procd_append_param command -p "${port}"
	procd_append_param command -debug "0"
	procd_append_param command -dbpath "${db_dir}"
	procd_append_param command -token "1"
	procd_append_param command -lang "zh"
	procd_set_param env GIN_MODE=release
	procd_set_param pidfile /var/run/gecoosac.pid
	procd_set_param stdout $log
    procd_set_param stderr $log
	procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
	procd_close_instance
}

stop_service(){
	init_conf
}

reload_service()
{
	stop
	start
}

service_triggers() {
	procd_add_reload_trigger "gecoosac"
}
